﻿using System;
using Castle.Core;
using Castle.Facilities.Startable;
using Castle.Windsor;
using NUnit.Framework;

namespace WindsorInitConfig {
    [TestFixture]
    public class StartStopDisposeTests2 {
        [Test]
        public void Test() {
            using (var container = new WindsorContainer()) {
                container.AddFacility<StartableFacility>();
                container.AddComponent<IEmailService, EmailService>();
                container.AddComponent<ICriticalService, CriticalService>();
                container.Resolve<ICriticalService>().DoStuff();
                container.Resolve<IEmailService>().SendEmail("Blah");
            }
        }

        internal class CriticalService : ICriticalService, IStartable {
            private readonly IEmailService email;

            public CriticalService(IEmailService email) {
                this.email = email;
            }

            public void Start() {}

            public void Stop() {
                // Should throw ComponentNotFoundException, as EmailService is already disposed and removed from the container
                email.SendEmail("Stopping");
            }

            public void DoStuff() {}
        }

        internal class EmailService : IEmailService {
            public void SendEmail(string message) {
                Console.WriteLine(message);
            }

            public void Dispose() {
                Console.WriteLine("EmailService Disposed.");
                GC.SuppressFinalize(this);
            }
        }

        internal interface ICriticalService {
            void DoStuff();
        }

        internal interface IEmailService : IDisposable {
            void SendEmail(string message);
        }
    }
}